{% extends 'home.html' %}

{% block content %}
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">图书展示</h3>
        </div>
        <div class="panel-body">
            <a class="btn btn-success" href="/add_book/" role="button">添加</a>
            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>图书名</th>
                    <th>价格</th>
                    <th>作者</th>
                    <th>出版时间</th>
                    <th>出版社</th>
                    <th>action</th>
                </tr>
                </thead>
                <tbody>
                {% for book_obj in book_objs %}
                    <tr>
                        <td>{{ book_obj.title }}</td>
                        <td>{{ book_obj.price }}</td>
                        <td>
                            {% for author_obj in book_obj.authors.all %}
                                {% if forloop.last %}
                                    {{ author_obj.name }}
                                {% else %}
                                    {{ author_obj.name }} &nbsp;&nbsp;|
                                {% endif %}

                            {% endfor %}

                        </td>
                        <td>{{ book_obj.publish_date|date:'Y-m-d' }}</td>
                        <td>{{ book_obj.publish.name }}</td>
                        <td>
                            <a class="btn btn-primary" href="{% url 'edit_data' book_obj.pk %}" role="button">编辑</a>
                            <button class="btn btn-danger btn_del" type="button" id='hidden_{{ book_obj.pk }}' aa='{{ book_obj.pk }}'>删除
                            </button>
                            {#                            <input type="hidden" id = 'hidden_{{ book_obj.pk }}' value="{{ book_obj.pk }}">#}
                        </td>
                    </tr>
                {% endfor %}
                <script>

                    $('.btn_del').click(function () {
                        var id = $(this).attr('aa')
                        {#console.log(id)#}
                        //询问框
                        layer.confirm('确定要删除吗？', {
                            btn: ['确定', '取消'] //按钮
                        }, function () {
                            //操作
                            $.ajax({
                                url: '/delete_data/',
                                type: 'post',
                                data: {'id': id},
                                success: function (res) {
                                    if (res.code == 200){
                                        layer.msg(res.msg, {icon: 1},function (){
                                            location.reload()
                                        })

                                    }

                                }
                            })
                            layer.msg('删除成功', {icon: 1});
                        });
                    })
                </script>

                </tbody>
            </table>
        </div>
    </div>
{% endblock %}